// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

package syscomp.entities;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URI;

import core.CodeGenerator;
import core.Entity;


public class Memory extends Entity {
	private final int bandwidth;
	private final int latency;
	private final String read;
	private final String write;
	private final String core;
	/**
	 * @param bandwidth
	 * @param latency
	 * @param read
	 * @param write
	 */
	public Memory(String name, final String core, URI fileURI, final int bandwidth, final int latency, final String read, final String write) {
		this.setName(name);
		this.core = core;
		this.setFileURI(fileURI);
		this.bandwidth = bandwidth;
		this.latency = latency;
		this.read = read;
		this.write = write;
	}
	public static Memory create(String n, String c, URI f, int bw, int lat, String r, String w) {
		return new Memory(n, c, f, bw, lat, r, w);
	}
	/**
	 * @return the bandwidth
	 */
	public int getBandwidth() {
		return bandwidth;
	}
	/**
	 * @return the latency
	 */
	public int getLatency() {
		return latency;
	}
	/**
	 * @return the read
	 */
	public String getRead() {
		return read;
	}
	/**
	 * @return the write
	 */
	public String getWrite() {
		return write;
	}
	/**
	 * @return the core
	 */
	public String getCore() {
		return core;
	}
	public void generateIncludes(PrintStream file) {
		file.println("#include \"lc_" + this.getName() + "_Memory.h\"");
	}
	/* (non-Javadoc)
	 * @see syscomp.entities.Entity#generateCode()
	 */
	@Override
	public void generateCode(CodeGenerator cg) {
		String filename = "lc_" + this.getName() + "_Memory.h";
		FileOutputStream out = null;
		PrintStream file = null;
		try {
		    out = new FileOutputStream(filename);
		    file = new PrintStream(out);
		}
		catch (FileNotFoundException e) {
		    System.err.println("Specified file " + filename + " does not exist.");
		    System.exit(1);
		}

		// Print guards
		file.println("#ifndef LC_" + this.getName().toUpperCase() + "_H_");
		file.println("#define LC_" + this.getName().toUpperCase() + "_H_");
		file.println();

		// Print standard header info
		file.println("#include <assert.h>");
		file.println("#include <lagniappe/Memory.h>");
		file.println();
		
		file.println("#include \"" + this.getFileURI() + "\"");
		
		// Start class definition
		
		file.println("class " + this.getName() + " : public lagniappe::Memory {");
		
		// private member variables
		file.println("private:");
		file.println("\t" + this.core + " coreMem;");
		
		// public method definitions
		file.println("public:");
		// queue access
		file.println("\tvirtual void put(void * data) {coreMem." + this.write + "(data);}");
		file.println("\tvirtual void * get() {return coreMem." + this.read + "();}");

		// Close class definition
		
		file.println("};");
		
		// Close guards
		file.println("#endif");
		file.println();
		
	}
}
